package it.amicosmanettone.finance.stocky.util;

import it.amicosmanettone.finance.stocky.bean.PortfolioBean;
import it.amicosmanettone.finance.stocky.bean.SingleStockBean;
import it.amicosmanettone.finance.yahoo.jsonbean.ResultSet;
import it.amicosmanettone.finance.yahoo.jsonbean.SearchResults;
import it.amicosmanettone.finance.yahoo.jsonbean.SearchSingleResult;

import java.util.ArrayList;

import android.util.Log;

public class FinanceUtil {

	public static boolean IS_LITE_APP_VERSION = false;
	public static int LITE_APP_VERSION_MAX_PORTFOLIO = 1;
	public static int LITE_APP_VERSION_MAX_STOCK_FOR_PORTFOLIO = 3;
	public static String lastErrorType = "NO_ERROR";

	private static ArrayList<PortfolioBean> allPortfolio = new ArrayList<PortfolioBean>();

	/**
	 * Return complete list of all portfolios
	 * 
	 * @return
	 */
	public static ArrayList<PortfolioBean> getAllPortfolio() {
		return allPortfolio;
	}// End method..

	public static void setAllPortfolio(ArrayList<PortfolioBean> allPortfolio) {
		FinanceUtil.allPortfolio = allPortfolio;
	}

	/**
	 * Return true created without problem, false otherwise if name already
	 * exists
	 * 
	 * @param namePortfolio
	 * @return
	 */
	public static String createNewPortfolio(String namePortfolio) {
		if (IS_LITE_APP_VERSION
				&& (allPortfolio.size() == LITE_APP_VERSION_MAX_PORTFOLIO)) {
			FinanceUtil.setLastErrorType("APP_LITE_LIMIT_PORTFOLIO_NUMBER");
			Log.v("FinanceUtil", "createNewPortfolio: App Lite Limit");
			return (ErrorCode.ERR_NO_PREMIUM);
		}// End if..
		if (!nameAlreadyExist(namePortfolio)) {// If not already exists
			PortfolioBean nuovoPortfolio = new PortfolioBean(namePortfolio);
			allPortfolio.add(nuovoPortfolio);
			Log.v("FinanceUtil", "createNewPortfolio: true " + namePortfolio
					+ " SIZE: " + FinanceUtil.getAllPortfolio().size());
			return (ErrorCode.NO_ERROR);
		} else {
			Log.v("FinanceUtil", "createNewPortfolio: ERROR " + namePortfolio);
			return (ErrorCode.ERR_EXIST);
		}// End if..else...
	}// End method...

	/**
	 * Return true if deleted without problem, false otherwise if it can found
	 * portfolio name...
	 * 
	 * @param namePortfolio
	 * @return
	 */
	public static boolean deletePortfolio(String namePortfolio) {
		Log.v("FinanceUtil", "I AM IN DELETE");
		if (nameAlreadyExist(namePortfolio)) {// if exists...
			for (int i = 0; i < allPortfolio.size(); i++) {

				if (allPortfolio.get(i).getPortfolioName()
						.equalsIgnoreCase(namePortfolio)) {
					allPortfolio.remove(allPortfolio.get(i));
					break;
				}
			}

			Log.v("FinanceUtil", "deletePortfolio: true " + namePortfolio
					+ " NEW SIZE: " + allPortfolio.size());
			return (true);
		} else {
			Log.v("FinanceUtil", "deletePortfolio: ERROR " + namePortfolio);
			return (false);
		}// End if..else...
	}// End method...

	/**
	 * Change name of an existing portfolio
	 * 
	 * @param portfolio
	 * @param newName
	 * @return
	 */
	public static boolean changePortfolioName(PortfolioBean portfolio,
			String newName) {
		if (!nameAlreadyExist(newName)) {// If not already exists
			portfolio.setPortfolioName(newName);
			Log.v("FinanceUtil", "changePortfolioName: true " + newName);
			return (true);
		} else {
			Log.v("FinanceUtil", "createNewPortfolio: ERROR " + newName);
			return (false);
		}// End if..else...
	}// End method...

	/**
	 * Search if name is already used in portfolio set
	 * 
	 * @param namePortfolio
	 * @return
	 */
	private static boolean nameAlreadyExist(String namePortfolio) {
		boolean found = false;
		for (int i = 0; i < allPortfolio.size(); i++) {
			PortfolioBean actual = allPortfolio.get(i);
			if (actual.getPortfolioName().equalsIgnoreCase(namePortfolio)) {
				found = true;
				break;
			}// End if..
		}// End for..
		return (found);
	}// End method...

	
	
	public static double retrieveQuote(String name) {
		Double response = SendData.retrieveQuoteNuovo(name);
		Log.v("FinanceUtil", "retrieveQuote | Response: " + response);
		return (response);
	}// End method..

	/**
	 * Return currency rate between two currency
	 * 
	 * @param curr1
	 * @param curr2
	 * @return
	 */
	public static double retrieveRateCurrency(String curr1, String curr2) {
		Double response = SendData.retrieveChangeCurrency(curr1, curr2);
		Log.v("FinanceUtil", "retrieveRateCurrency | Response: " + response);
		return (response);
	}// End method..

	/**
	 * Update stocks value of all portfolios
	 * 
	 * @return
	 */
	public static boolean updateAllPortfolios() {
		boolean allOk=true;
		boolean singleOk=true;
		ArrayList<PortfolioBean> listPortfolio=FinanceUtil.getAllPortfolio();
		for(int i=0;i<listPortfolio.size();i++){
			singleOk=listPortfolio.get(i).updatePortfolio();
			if(!singleOk){
				allOk=false;
			}//End if...
		}//End if...
		return (allOk);
	}// End method..

	public static ArrayList<SingleStockBean> searchStocksOnline(String query) {

		// ArrayList<SingleStockBean>
		// results=SendData.searchStocksOnline(query);

		ArrayList<SingleStockBean> arrayList = new ArrayList<SingleStockBean>();
		ResultSet results = SendData.searchStocksOnline(query);

		if (results != null) {
			Log.v("SENDDATA", "results: "
					+ results.getResultSet().getResult().size());
		} else {
			Log.v("SENDDATA", "results.getResult() null");
		}

		SearchResults list = results.getResultSet();
		SingleStockBean newStock = null;
		SearchSingleResult single = null;
		for (int i = 0; i < list.getResult().size(); i++) {
			single = list.getResult().get(i);
			newStock = new SingleStockBean(single.getName());
			newStock.setStockSymbol(single.getSymbol());
			
			//if(!newStock.getStockCurrency().equalsIgnoreCase("EUR")){
				newStock.setStockCurrency(FinanceUtil.getCurrencyByExchangeMarket(single.getExchDisp()));
			//}
			
			arrayList.add(newStock);
		}

		// SingleStockBean A=new SingleStockBean("RISULTATO 1");
		// SingleStockBean B=new SingleStockBean("RISULTATO 2");
		// ArrayList<SingleStockBean> list=new ArrayList<SingleStockBean>();
		// list.add(A);
		// list.add(B);
		return (arrayList);
	}

	/**
	 * Update stokcs value of only one portfolio
	 * 
	 * @param name
	 * @return
	 */
	public static boolean updateOnlyPortfolio(String name) {
		return (true);
	}// End method..

	public static String getLastErrorType() {
		return lastErrorType;
	}

	public static void setLastErrorType(String lastErrorType) {
		FinanceUtil.lastErrorType = lastErrorType;
	}

	public static String getCurrencyByExchangeMarket(String market) {
		return ("USD");
	}

}// End class...
